<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input v-model="queryParams.ipAddress" placeholder="请输IP地址" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="数据库名称" prop="databaseName">
        <el-input v-model="queryParams.databaseName" placeholder="请输入数据库名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="链接名称" prop="linkName">
        <el-input v-model="queryParams.linkName" placeholder="请输入链接名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['datav:datasourse:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['datav:datasourse:edit']">修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['datav:datasourse:remove']">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sourseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" prop="id" align="center" width="50" />
      <el-table-column label="数据库类型" prop="databaseType" align="center" :show-overflow-tooltip="true" width="100" />
      <el-table-column label="IP地址" prop="ipAddress" align="center" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="端口" prop="port" align="center" width="100" />
      <el-table-column label="数据库名称" prop="databaseName" align="center" width="150" />
      <el-table-column label="链接名称" prop="linkName" align="center" width="150" />
      <el-table-column label="用户名" prop="username" align="center" width="120" />
      <el-table-column label="密码" prop="password" align="center" width="150" />
      <el-table-column label="备注" prop="remark" align="center" width="120" />
      <el-table-column label="创建人" prop="createBy" align="center" width="170" />
      <el-table-column label="创建时间" align="center" prop="createTime"  width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:role:edit']" >修改</el-button >
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:role:remove']" >删除</el-button >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数据库类型" prop="databaseType" label-width="100px">
          <!-- <el-input v-model="form.databaseType" placeholder="请输入数据库类型" /> -->
          <el-select v-model="form.databaseType" placeholder="请选择数据库类型">
            <el-option label="MySQL" value="mysql"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress" label-width="100px">
          <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="端口" prop="port" label-width="100px">
          <el-input v-model="form.port" placeholder="请输入端口号"  :min="0" />
        </el-form-item>
        <el-form-item label="数据库名称" prop="databaseName" label-width="100px">
          <el-input v-model="form.databaseName" placeholder="请输入数据库名称" :min="0" />
        </el-form-item>
        <el-form-item label="链接名称" prop="linkName" label-width="100px">
          <el-input v-model="form.linkName" placeholder="请输入链接名称" :min="0" />
        </el-form-item>
        <el-form-item label="用户名" prop="username" label-width="100px">
          <el-input v-model="form.username" placeholder="请输入用户名" :min="0" />
        </el-form-item>
        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" :min="0" />
        </el-form-item>
        <el-form-item label="备注" label-width="100px">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSourse, getSourse, addSourse, updateSourse, delSourse } from "@/api/tool/datav/sourse";

export default {
  name: "Role",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      sourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipAddress: undefined,
        databaseName: undefined,
        linkName: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        databaseType: [
          { required: true, message: "数据库类型不能为空", trigger: "blur" }
        ],
        ipAddress: [
          { required: true, message: "ip地址不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口号不能为空", trigger: "blur" }
        ],
        databaseName: [
          { required: true, message: "数据库名称不能为空", trigger: "blur" }
        ],
        linkName: [
          { required: true, message: "链接名称不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询数据源列表 */
    getList() {
      this.loading = true;
      listSourse(this.queryParams).then((response) => {
        this.sourseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        id: undefined,
        databaseType: undefined,
        ipAddress: undefined,
        port: undefined,
        databaseName: undefined,
        linkName: undefined,
        username: undefined,
        password: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      //   this.$nextTick(() => {
      //     this.getRoleMenuTreeselect(roleId);
      //   });
      getSourse(id).then((response) => {
        console.log("要修改的信息id为：", id);
        console.log("列表信息：", response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改数据源";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSourse(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSourse(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function () {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then((response) => {
          if (response.code === 200) {
            this.msgSuccess("修改成功");
            this.openDataScope = false;
            this.getList();
          } else {
            this.msgError(response.msg);
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(ids);
      this.$confirm(
        '是否确认删除编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(function () {
          return delSourse(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {});
    },
  },
};
</script>